/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AtividadeView.java
 *
 * Created on 25/09/2009, 20:08:50
 */

package view.processo;

import Util.manipulaConexao;
import controler.processo.AtividadeControler;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import model.processo.*;
import model.sistema.Usuario;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.sql.SQLException.*;
import Util.TextDocument;
import Util.FabricaConexoes;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author D4nG3Rr
 */
public class AtividadeView extends javax.swing.JInternalFrame {

    /** Creates new form AtividadeView */
    public AtividadeView() {
        atividadedata = new AtividadeData();
        atividadeseldata = new AtividadeSelectData();
        initComponents();
        jtbatividades.getSelectionModel().addListSelectionListener(selectionListener);
        jtbatividades.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jtnome.setDocument(new TextDocument(44));
        jtdescricao.setDocument(new TextDocument(299));
        jtbfinalizacao.getSelectionModel().addListSelectionListener(selectionListenerfin);
        jtbfinalizacao.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtbfinalizacao = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jtnome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtdescricao = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jtcodigo = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jrativo = new javax.swing.JRadioButton();
        jrinativo = new javax.swing.JRadioButton();
        jbadicionarfin = new javax.swing.JButton();
        jbremoverfin = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jbincluir = new javax.swing.JButton();
        jbalterar = new javax.swing.JButton();
        jbexcluir = new javax.swing.JButton();
        jbcancelar = new javax.swing.JButton();
        jbgravar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jtpesquisar = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbatividades = new javax.swing.JTable();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Cadastro de Atividades");
        setVisible(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados da atividade"));

        jtbfinalizacao.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(jtbfinalizacao);

        jLabel1.setText("Nome:");

        jtnome.setColumns(8);
        jtnome.setEnabled(false);

        jLabel2.setText("Descrição:");

        jtdescricao.setColumns(20);
        jtdescricao.setLineWrap(true);
        jtdescricao.setRows(3);
        jtdescricao.setEnabled(false);
        jScrollPane1.setViewportView(jtdescricao);

        jLabel6.setText("Código:");

        jtcodigo.setEnabled(false);

        jLabel7.setText("Finalizações:");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Status"));

        jrativo.setText("Ativo");
        jrativo.setEnabled(false);
        jrativo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrativoActionPerformed(evt);
            }
        });

        jrinativo.setText("Inativo");
        jrinativo.setEnabled(false);
        jrinativo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrinativoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jrativo)
                    .addComponent(jrinativo))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jrativo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jrinativo)
                .addContainerGap(11, Short.MAX_VALUE))
        );

        jbadicionarfin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/add_obj.gif"))); // NOI18N
        jbadicionarfin.setEnabled(false);
        jbadicionarfin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbadicionarfinActionPerformed(evt);
            }
        });

        jbremoverfin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/delete_obj.gif"))); // NOI18N
        jbremoverfin.setEnabled(false);
        jbremoverfin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbremoverfinActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jtnome, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2)
                    .addComponent(jLabel7)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbadicionarfin)
                            .addComponent(jbremoverfin, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtnome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jbadicionarfin, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbremoverfin)
                        .addGap(30, 30, 30))))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jbincluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/newfile_wiz.gif"))); // NOI18N
        jbincluir.setText("Incluir");
        jbincluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbincluirActionPerformed(evt);
            }
        });

        jbalterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/synced.gif"))); // NOI18N
        jbalterar.setText("Alterar");
        jbalterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbalterarActionPerformed(evt);
            }
        });

        jbexcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/delete_edit.gif"))); // NOI18N
        jbexcluir.setText("Excluir");
        jbexcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbexcluirActionPerformed(evt);
            }
        });

        jbcancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/progress_stop.gif"))); // NOI18N
        jbcancelar.setText("Cancelar");
        jbcancelar.setEnabled(false);
        jbcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbcancelarActionPerformed(evt);
            }
        });

        jbgravar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/save_edit.gif"))); // NOI18N
        jbgravar.setText("Gravar");
        jbgravar.setEnabled(false);
        jbgravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbgravarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jbincluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbalterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbexcluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbcancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbgravar)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(jbincluir)
                .addComponent(jbalterar)
                .addComponent(jbexcluir)
                .addComponent(jbcancelar)
                .addComponent(jbgravar))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Atividades"));

        jLabel5.setText("Pesquisar:");

        jtpesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtpesquisarActionPerformed(evt);
            }
        });
        jtpesquisar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jtpesquisarFocusGained(evt);
            }
        });
        jtpesquisar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtpesquisarKeyReleased(evt);
            }
        });

        jtbatividades.setModel(atividadeseldata);
        jtbatividades.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(jtbatividades);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
                    .addComponent(jtpesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtpesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-545)/2, (screenSize.height-661)/2, 545, 661);
    }// </editor-fold>//GEN-END:initComponents

    private void jrativoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrativoActionPerformed
        if(jrativo.isSelected()) jrinativo.setSelected(false);
    }//GEN-LAST:event_jrativoActionPerformed

    private void jrinativoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrinativoActionPerformed
        if(jrinativo.isSelected()) jrativo.setSelected(false);
    }//GEN-LAST:event_jrinativoActionPerformed

    private void jbcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbcancelarActionPerformed
        Status(1);
        limpaDados();
        atividadedata.atualizaDados();
        atividadeseldata.atualizaDados();
        selectionListener.valueChanged(null);
    }//GEN-LAST:event_jbcancelarActionPerformed

    private void jtpesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtpesquisarActionPerformed
       
    }//GEN-LAST:event_jtpesquisarActionPerformed

    private void jbincluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbincluirActionPerformed
            Status(2);
            jrativo.setSelected(true);
            jrinativo.setSelected(false);
            limpaDados();
            jbadicionarfin.setEnabled(false);
            jbremoverfin.setEnabled(false);
            Object[] objeto = new Object[2];
            objeto[0] = "Código";
            objeto[1] = "Finalizações";
            jtbfinalizacao.setModel(new DefaultTableModel(objeto, 0));
            
    }//GEN-LAST:event_jbincluirActionPerformed

    private void jbgravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbgravarActionPerformed

            if(!montaAtividade()) return;

            atcontroler = new AtividadeControler();
            if(!atcontroler.gravarAtividade(atividade))return;
            Status(1);
            atividadedata.atualizaDados();
            atividadeseldata.atualizaDados();
            jtbatividades.revalidate();
            jtbatividades.repaint();
            selectionListener.valueChanged(null);
    }//GEN-LAST:event_jbgravarActionPerformed

    private void jbalterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbalterarActionPerformed
            int linha = jtbatividades.getSelectedRow();
            selectionListener.valueChanged(null);
            Status(2);

    }//GEN-LAST:event_jbalterarActionPerformed

    private void jbexcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbexcluirActionPerformed
            int linha = jtbatividades.getSelectedRow();
            atcontroler = new AtividadeControler();
            atividade = atividadedata.getAtividade(linha);
            if(!atcontroler.excluirAtividade(atividade)) return;
            atividadedata.atualizaDados();
            atividadeseldata.atualizaDados();
            jtbatividades.revalidate();
            jtbatividades.repaint();
            limpaDados();
            selectionListener.valueChanged(null);
    }//GEN-LAST:event_jbexcluirActionPerformed

    private void jtpesquisarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtpesquisarKeyReleased
        TableRowSorter<TableModel> sorter;
        sorter = new TableRowSorter<TableModel>(jtbatividades.getModel());
        jtbatividades.setRowSorter(sorter);
        sorter.setRowFilter(javax.swing.RowFilter.regexFilter(jtpesquisar.getText(),1));

    }//GEN-LAST:event_jtpesquisarKeyReleased

    private void jtpesquisarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtpesquisarFocusGained
        
            
    }//GEN-LAST:event_jtpesquisarFocusGained

    private void jbremoverfinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbremoverfinActionPerformed
        selectionListenerfin.valueChanged(null);
        if(idativ == 0 || idfin == 0) return;
        AtividadeDao ativdao = new AtividadeDao();
        try{
            ativdao.deleteAtividadeFinalizacao(idativ, idfin);
            selectionListenerfin.valueChanged(null);
            selectionListener.valueChanged(null);
        }catch(SQLException sql)
        {
            JOptionPane.showMessageDialog(null, sql.getMessage(),"Erro",0);
        }
    }//GEN-LAST:event_jbremoverfinActionPerformed

    private void jbadicionarfinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbadicionarfinActionPerformed
        new ListaFinalView(null, true, this).setVisible(true);
        selectionListener.valueChanged(null);
    }//GEN-LAST:event_jbadicionarfinActionPerformed
    public void insereAtivFin(int idativ, int idfin)
    {
        try
        {
           AtividadeDao ativ =  new AtividadeDao();
           ativ.insertAtividadeFinalizacao(idativ, idfin);
           
        }catch(SQLException sql)
        {
            JOptionPane.showMessageDialog(null, sql.getMessage(),"Erro",0);
        }
    }
    
    private void Status(int opcao)
    {
        switch(opcao)
        {
            case 1 :
                
                    jbincluir.setEnabled(true);
                    jbalterar.setEnabled(true);
                    jbexcluir.setEnabled(true);
                    jbgravar.setEnabled(false);
                    
                    jbcancelar.setEnabled(false);
                    jtdescricao.setEnabled(false);
                    jtnome.setEnabled(false);
                    jtpesquisar.setEnabled(true);
                    jrativo.setEnabled(false);
                    jrinativo.setEnabled(false);
                    jbadicionarfin.setEnabled(false);
                    jbremoverfin.setEnabled(false);
                    break;
            case 2 :
                
                    jbincluir.setEnabled(false);
                    jbalterar.setEnabled(false);
                    jbexcluir.setEnabled(false);
                    jbgravar.setEnabled(true);
                    
                    jbcancelar.setEnabled(true);
                    jtdescricao.setEnabled(true);
                    jtnome.setEnabled(true);
                    jtpesquisar.setEnabled(false);
                    jrativo.setEnabled(true);
                    jrinativo.setEnabled(true);
                    jbadicionarfin.setEnabled(true);
                    jbremoverfin.setEnabled(true);
                    break;
            default :
                    break;
        }
    }
    private void limpaDados()
    {
        jtcodigo.setText("");
        jtdescricao.setText("");
        
        jtnome.setText("");
    }

    private boolean montaAtividade()
    {

        if(jtdescricao.getText().compareToIgnoreCase("") == 0)
        {
            JOptionPane.showMessageDialog(null,"Campo descrição não pode ser nulo,verifique!","Erro",0);
            return false;
        }
        if(jtnome.getText().compareToIgnoreCase("") == 0)
        {
            JOptionPane.showMessageDialog(null,"Campo nome não pode ser nulo,verifique!","Erro",0);
            return false;
        }
        

        atividade = new Atividade();
        Usuario usuario = manipulaConexao.getUsuarioLogado();
        try{

            atividade.setId(Integer.parseInt(jtcodigo.getText()));

        }catch(NumberFormatException e)
        {
            atividade.setId(0);
        }
        if(jrativo.isSelected())
        {
            atividade.setStatus(1);
        }else{
               atividade.setStatus(0);
                }
        atividade.setDescricao(jtdescricao.getText());
        atividade.setNome(jtnome.getText());
        atividade.setAutor(usuario.getId());
        atividade.setUserModif(usuario.getId());
        
        return true;

    }
    private ListSelectionListener selectionListener = new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e)
        {
            
            int linha = jtbatividades.getSelectedRow();
            if(linha == -1) return;
            atividadeseldata.atualizaDados();
            atividadedata.atualizaDados();
            
            atividade = atividadedata.getAtividadeId(Integer.valueOf(String.valueOf(jtbatividades.getValueAt(linha, 0))));
            jtcodigo.setText(String.valueOf(atividade.getId()));
            jtnome.setText(atividade.getNome());
            jtdescricao.setText(atividade.getDescricao());
            if(atividade.getStatus() == 1)
            {
                jrativo.setSelected(true);
                jrinativo.setSelected(false);
            }
            else
                {
                   jrativo.setSelected(false);
                    jrinativo.setSelected(true);
                }
         String sql = "Select Código, Finalizações FROM ativ_fin WHERE Atividade like '"+atividade.getNome()+"'";
	 manipulaConexao.preenchetable(jtbfinalizacao, sql);
         FabricaConexoes.closeConnection();
        }

        
    };


    private ListSelectionListener selectionListenerfin = new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e)
        {

            int linha = jtbfinalizacao.getSelectedRow();
            if(linha == -1) return;
            idfin = Integer.valueOf(String.valueOf(jtbfinalizacao.getValueAt(linha, 0)));
            idativ = atividade.getId();
        }


    };

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton jbadicionarfin;
    private javax.swing.JButton jbalterar;
    private javax.swing.JButton jbcancelar;
    private javax.swing.JButton jbexcluir;
    private javax.swing.JButton jbgravar;
    private javax.swing.JButton jbincluir;
    private javax.swing.JButton jbremoverfin;
    private javax.swing.JRadioButton jrativo;
    private javax.swing.JRadioButton jrinativo;
    private javax.swing.JTable jtbatividades;
    private javax.swing.JTable jtbfinalizacao;
    public javax.swing.JTextField jtcodigo;
    private javax.swing.JTextArea jtdescricao;
    private javax.swing.JTextField jtnome;
    private javax.swing.JTextField jtpesquisar;
    // End of variables declaration//GEN-END:variables
    
    public AtividadeData atividadedata;
    private Atividade atividade;
    private AtividadeControler atcontroler;
    private AtividadeSelect atividadesel;
    private  AtividadeSelectDao atividadeseldao;
    private AtividadeSelectData atividadeseldata;
    int idativ,idfin;

}
